﻿--[[	$Id: oUF_cDebuff.lua 2488 2012-09-10 05:21:32Z sdkyron@gmail.com $	]]

if not oUF then return end

local _, oUF_Caellian = ...

oUF_Caellian.eventFrame = CreateFrame("Frame", nil, UIParent)

local playerClass = caelLib.playerClass

local canDispel = {
	DRUID = {Curse = true, Poison = true},
	MAGE = {Curse = true},
	MONK = {Poison = true, Disease = true}, 
	PALADIN = {Poison = true, Disease = true}, 
	PRIEST = {Disease = true, Magic = true},
	SHAMAN = {Curse = true}
}

oUF_Caellian.eventFrame:RegisterEvent("PLAYER_ENTERING_WORLD")
oUF_Caellian.eventFrame:RegisterEvent("CHARACTER_POINTS_CHANGED")
oUF_Caellian.eventFrame:RegisterEvent("ACTIVE_TALENT_GROUP_CHANGED")
oUF_Caellian.eventFrame:SetScript("OnEvent", function()
    if playerClass == "DRUID" or playerClass == "PALADIN" or playerClass == "SHAMAN" then
        local tab, index

        if playerClass == "DRUID" then
            tab, index = 3, 17
        elseif playerClass == "PALADIN" then
            tab, index = 1, 14
        elseif playerClass == "SHAMAN" then
            tab, index = 3, 12
        end

        local _, _, _, _, rank = GetTalentInfo(tab, index)

        canDispel[playerClass].Magic = rank == 1 and true
    end
end)

local dispelList = canDispel[playerClass] or {}

local DebuffTypeColor = {}

for k, v in pairs(_G["DebuffTypeColor"]) do
	DebuffTypeColor[k] = v
end

local backupColor = {r = 0.69, g = 0.31, b = 0.31}

setmetatable(DebuffTypeColor, {__index = function() return backupColor end})

local null = {""}
local spellcache = setmetatable({}, {__index = function(t, spellId) 
	local spell = {GetSpellInfo(spellId)} 

	if GetSpellInfo(spellId) then
	    t[spellId] = spell
	    return spell
	end

        t[spellId] = null
	return null
end
})

--local GetSpellName = function(spellId)
--	return GetSpellInfo(spellId)
--end

local GetSpellName = function(spell)
    return unpack(spellcache[spell])
end

local whiteList = {
-- Baradin Hold
	-- Argaloth
	[GetSpellName(95173)] = true,	-- Consuming Darkness
-- Blackwing Descent
	-- Magmaw
	[GetSpellName(94679)] = true,	-- Parasitic Infection
	[GetSpellName(94617)] = true,	-- Mangle
	[GetSpellName(91911)] = true,	-- Constricting Chains
	-- Omintron Defense System
	[GetSpellName(91433)] = true,	-- Lightning Conductor
	[GetSpellName(91535)] = true,	-- Flamethrower
	[GetSpellName(80161)] = true,	-- Chemical Cloud
	[GetSpellName(92035)] = true,	-- Acquiring Target
	[GetSpellName(79835)] = true,	-- Poison Soaked Shell
	[GetSpellName(91555)] = true,	-- Power Generator
	[GetSpellName(80094)] = true,	-- Fixate
	[GetSpellName(91521)] = true,	-- Incineration Security Measure
	[GetSpellName(92048)] = true,	-- Shadow Infusion
	[GetSpellName(92053)] = true,	-- Shadow Conductor
	-- Maloriak
	[GetSpellName(92754)] = true,	-- Engulfing Darkness
	[GetSpellName(77699)] = true,	-- Flash Freeze
	[GetSpellName(77760)] = true,	-- Biting Chill
	[GetSpellName(92971)] = true,	-- Consuming Flames
	[GetSpellName(92989)] = true,	-- Rend
	[GetSpellName(78617)] = true,	-- Fixate
	-- Atramedes
	[GetSpellName(92423)] = true,	-- Searing Flame
	[GetSpellName(92485)] = true,	-- Roaring Flame
	[GetSpellName(92407)] = true,	-- Sonic Breath
	[GetSpellName(78092)] = true,	-- Tracking
	-- Chimaeron
	[GetSpellName(82881)] = true,	-- Break
	[GetSpellName(89084)] = true,	-- Low Health
	-- Nefarian
	[GetSpellName(81114)] = true,	-- Magma
	[GetSpellName(94128)] = true,	-- Tail Lash
	[GetSpellName(79339)] = true,	-- Explosive Cinders
	[GetSpellName(79318)] = true,	-- Dominion
-- The Bastion of Twilight
	-- Halfus Wyrmbreaker
	[GetSpellName(83710)] = true,	-- Furious Roar
	[GetSpellName(39171)] = true,	-- Malevolent Strikes
	-- Valiona & Theralion
	[GetSpellName(86505)] = true,	-- Fabulous Flames
	[GetSpellName(86840)] = true,	-- Devouring Flames
	[GetSpellName(92878)] = true,	-- Blackout
	[GetSpellName(95639)] = true,	-- Engulfing Magic
	[GetSpellName(93051)] = true,	-- Twilight Shift
	[GetSpellName(92886)] = true,	-- Twilight Zone
	[GetSpellName(88518)] = true,	-- Twilight Meteorite
	-- Twilight Ascendant Council
	[GetSpellName(82660)] = true,	-- Burning Blood
	[GetSpellName(82665)] = true,	-- Heart of Ice
	[GetSpellName(82762)] = true,	-- Waterlogged
	[GetSpellName(83099)] = true,	-- Lightning Rod
	[GetSpellName(92488)] = true,	-- Gravity Crush
	[GetSpellName(83500)] = true,	-- Swirling Winds
	[GetSpellName(83581)] = true,	-- Grounded
	[GetSpellName(92505)] = true,	-- Frozen
	[GetSpellName(92511)] = true,	-- Hydro Lance
	[GetSpellName(92518)] = true,	-- Flame Torrent
	[GetSpellName(92075)] = true,	-- Gravity Core
	-- Cho'gall
	[GetSpellName(93187)] = true,	-- Corrupted Blood
	[GetSpellName(82125)] = true,	-- Corruption: Malformation
	[GetSpellName(82170)] = true,	-- Corruption: Absolute
	[GetSpellName(93200)] = true,	-- Corruption: Sickness
	[GetSpellName(82411)] = true,	-- Debilitating Beam
	[GetSpellName(91317)] = true,	-- Worshipping
	[GetSpellName(86028)] = true,	-- Cho's Blast
	[GetSpellName(86029)] = true,	-- Gall's Blast
	-- Sinestra
	[GetSpellName(92956)] = true,	-- Wrack
-- Throne of the Four Winds
	-- Conclave of Wind
	-- Nezir
	[GetSpellName(93131)] = true,	-- Ice Patch
	-- Anshal
	[GetSpellName(86206)] = true,	-- Soothing Breeze
	[GetSpellName(93122)] = true,	-- Toxic Spores
	-- Rohash
	[GetSpellName(93058)] = true,	-- Slicing Gale
	-- Al'Akir
	[GetSpellName(87873)] = true,	-- Static Shock
	[GetSpellName(93260)] = true,	-- Ice Storm
	[GetSpellName(87856)] = true,	-- Squall Line
	[GetSpellName(88427)] = true,	-- Electrocute
	[GetSpellName(93295)] = true,	-- Lightning Rod
-- Firelands
	-- Beth'tilac
	[GetSpellName(99506)] = true,	-- Widows Kiss
	[GetSpellName(97202)] = true,	-- Fiery Web Spin
	[GetSpellName(49026)] = true,	-- Fixate
	[GetSpellName(97079)] = true,	-- Seeping Venom
	-- Lord Rhyolith
	[GetSpellName(98492)] = true,	-- Eruption
	-- Alysrazor
	[GetSpellName(101296)] = true,	-- Fieroblast
	[GetSpellName(100723)] = true,	-- Gushing Wound
	[GetSpellName(99389)]  = true,	-- Imprinted
	[GetSpellName(101729)] = true,	-- Blazing Claw
	[GetSpellName(100640)] = true,	-- Harsh Winds
	[GetSpellName(100555)] = true,	-- Smouldering Roots
	-- Shannox
	[GetSpellName(99837)] = true,	-- Crystal Prison
	[GetSpellName(99937)] = true,	-- Jagged Tear
	-- Baleroc
	[GetSpellName(99403)] = true,	-- Tormented
	[GetSpellName(99256)] = true,	-- Torment
	[GetSpellName(99252)] = true,	-- Blaze of Glory
	[GetSpellName(99516)] = true,	-- Countdown
	-- Majordomo Staghelm
	[GetSpellName(98450)] = true,	-- Searing Seeds
	-- Ragnaros
	[GetSpellName(99399)]  = true,	-- Burning Wound
	[GetSpellName(100293)] = true,	-- Lava Wave
	[GetSpellName(98313)]  = true,	-- Magma Blast
	[GetSpellName(100675)] = true,	-- Dreadflame
	[GetSpellName(99145)]  = true,	-- Blazing Heat
	[GetSpellName(100249)] = true,	-- Combustion
	[GetSpellName(99613)]  = true,	-- Molten Blast
	-- Trash
	[GetSpellName(99532)] = true,	-- Melt Armor
-- Other
	[GetSpellName(67479)] = true,	-- Impale
	[GetSpellName(5782)]  = true,	-- Fear
	[GetSpellName(84853)] = true,	-- Dark Pool
	[GetSpellName(91325)] = true,	-- Shadow Vortex
-- Dragon Soul
	-- Morchok
	[GetSpellName(103687)] = true,	-- Crush Armor
	[GetSpellName(103536)] = true,	-- Warning
	[GetSpellName(103534)] = true,	-- Danger
	[GetSpellName(108570)] = true,	-- Black Blood of the Earth
	-- Warlord Zon'ozz
	[GetSpellName(103434)] = true,	-- Disrupting shadows
	[GetSpellName(110306)] = true,	-- Black Blood of Go'rath
	-- Yor'sahj the Unsleeping
	[GetSpellName(104849)] = true,	-- Void bolt
	[GetSpellName(109389)] = true,	-- Deep Corruption
	-- Hagara the Stormbinder
	[GetSpellName(104451)] = true,	-- Ice Tomb
	[GetSpellName(105285)] = true,	-- Target (next Ice Lance)
	[GetSpellName(105316)] = true,	-- Ice Lance
	[GetSpellName(105289)] = true,	-- Shattered Ice
	[GetSpellName(105259)] = true,	-- Watery Entrenchment	
	[GetSpellName(105465)] = true,	-- Lightning Storm
	[GetSpellName(105369)] = true,	-- Lightning Conduit
	-- Ultraxion
	[GetSpellName(110068)] = true,	-- Fading light 
	[GetSpellName(106108)] = true,	-- Heroic will
	[GetSpellName(106415)] = true,	-- Twilight burst
	[GetSpellName(105927)] = true,	-- Faded Into Twilight
	[GetSpellName(106369)] = true,	-- Twilight shift
	-- Warmaster Blackhorn
	[GetSpellName(109204)] = true,	-- Twilight Barrage
	[GetSpellName(108046)] = true,	-- Shockwave
	[GetSpellName(108043)] = true,	-- Devastate
	[GetSpellName(107567)] = true,	-- Brutal strike
	[GetSpellName(107558)] = true,	-- Degeneration
	[GetSpellName(110214)] = true,	-- Consuming Shroud
	-- Spine of Deathwing
	[GetSpellName(105563)] = true,	-- Grasping Tendrils
	[GetSpellName(105490)] = true,	-- Fiery Grip
	[GetSpellName(105479)] = true,	-- Searing Plasma
	[GetSpellName(106199)] = true,	-- Blood corruption: death
	[GetSpellName(106200)] = true,	-- Blood corruption: earth
	[GetSpellName(106005)] = true,	-- Degradation
	-- Madness of Deathwing
	[GetSpellName(109603)] = true,	-- Tetanus
	[GetSpellName(109632)] = true,	-- Impale
	[GetSpellName(106794)] = true,	-- Shrapnel
	[GetSpellName(106385)] = true,	-- Crush
	[GetSpellName(105841)] = true,	-- Degenerative bite
	[GetSpellName(105445)] = true,	-- Blistering heat
}

local function GetDebuffType(unit, filter)
	if not UnitCanAssist("player", unit) then return end

	local dispelType, debuffIcon

	local i = 1
	local isWhiteList

	while true do
		local name, _, icon, _, debuffType = UnitAura(unit, i, "HARMFUL")

		if not icon then break end

		if dispelList[debuffType] or (not filter and debuffType) or whiteList[name] then

			dispelType = debuffType
			debuffIcon = icon

			if whiteList[name] then
				isWhiteList = true
				break
			end
		end

		i = i + 1
	end

	return dispelType, debuffIcon, isWhiteList
end

local function Update(self, event, unit)
	if self.unit ~= unit  then return end

	local dispelType, debuffIcon, isWhiteList = GetDebuffType(unit, self.cDebuffFilter)

	if self.cDebuffBackdrop then
		local color

		if debuffIcon then
			color = DebuffTypeColor[dispelType]
			self.cDebuffBackdrop:SetVertexColor(color.r, color.g, color.b, 1)
		else
			self.cDebuffBackdrop:SetVertexColor(0, 0, 0, 0)
		end
	end

	if self.cDebuff.icon then
		if debuffIcon then
			self.cDebuff.icon:SetTexture(debuffIcon)
			self.cDebuff.border:SetBackdropColor(0.5, 0.5, 0.5, 1)
			self.cDebuff.gloss:SetBackdropColor(0.25, 0.25, 0.25, 0.5)
		else
			self.cDebuff.icon:SetTexture(nil)
			self.cDebuff.border:SetBackdropColor(0.5, 0.5, 0.5, 0)
			self.cDebuff.gloss:SetBackdropColor(0.25, 0.25, 0.25, 0)
		end
	end
end

local function Enable(self)
	if not self.cDebuff then return end

	self:RegisterEvent("UNIT_AURA", Update)

	return true
end

local function Disable(self)
	if self.cDebuffBackdrop or self.cDebuff.icon then
		self:UnregisterEvent("UNIT_AURA", Update)
	end
end

oUF:AddElement("cDebuff", Update, Enable, Disable)